﻿using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace CollectionOperationKit
{
    [Icon("pack://application:,,,/CollectionOperationKit;component/Resources/ArrayIcon.png")]
    [Category("对象与集合操作")]
    public class ClientSideArrayCalc : Command
    {
        /// <summary>
        /// 在设计器中展示的插件名称
        /// </summary>
        /// <returns>易读的字符串</returns>
        public override string ToString()
        {

            if (Operation == SupportedOperations.SUM)
            {
                if (String.IsNullOrEmpty(OutParamaterName))
                {
                    return "数组统计计算"; // 命令列表中默认显示的名称
                }
                else
                {
                    return "数组统计计算（SUM）：" + OutParamaterName;
                }

            }
            else
            {

                if (String.IsNullOrEmpty(OutParamaterName))
                {
                    return "数组统计计算（" + Operation.ToString() + "）";
                }
                else
                {
                    return "数组统计计算（" + Operation.ToString() + "）：" + OutParamaterName;
                }
            }
        }

        /// <summary>
        /// 插件类型：设置为服务端命令插件
        /// </summary>
        /// <returns>插件类型枚举</returns>
        public override CommandScope GetCommandScope()
        {
            return CommandScope.ClientSide;
        }

        [OrderWeight(100)]
        [DisplayName("输入参数（数组）")]
        [FormulaProperty]
        public object InParamater { get; set; }

        [OrderWeight(110)]
        [DisplayName("操作")]
        [ComboProperty]
        [SearchableProperty]
        public SupportedOperations Operation { get; set; }

        [OrderWeight(120)]
        [DisplayName("属性名")]
        [Description("在该属性名中执行统计计算，%value%则使用数组元素本身。在数值计算时，null将按照0来处理。")]
        [FormulaProperty(AcceptsReturn = true)]
        public object OperationFieldName { get; set; }

        [OrderWeight(999)]
        [DisplayName("将结果返回到变量")]
        [ResultToProperty]
        public String OutParamaterName { get; set; }

        public enum SupportedOperations
        {
            [Description("SUM：求和")]
            SUM,
            [Description("COUNT：计数，仅统计所有非空项目")]
            COUNT,
            [Description("AVG：算术平均值")]
            AVG,
            [Description("MAX：取最大值")]
            MAX,
            [Description("MIN：取最小值")]
            MIN
        }
    }
}
